package net.zoneland.zrdp.framework.config;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class JasyptConfig {
    @Value("${jasypt.encryptor.password}")
    private String jasyptPassword;

    private static final String KEY_PEFIX = "3kOahbPHCwee6ApI2Oc2tZVO";
    /**
     * 配置StringEncryptor
     */
    @Bean("jasyptStringEncryptor")
    public StringEncryptor stringEncryptor() {
        final PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        final SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(KEY_PEFIX + jasyptPassword);
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        return encryptor;
    }

    /**
     * @param jasyptPassword the jasyptPassword to set
     */
    public void setJasyptPassword(final String jasyptPassword) {
        this.jasyptPassword = jasyptPassword;
    }
}
